package com.lagou.task.servlet;

import com.lagou.task.entity.Student;
import com.lagou.task.entity.SysClass;
import com.lagou.task.service.ClassService;
import com.lagou.task.service.StudentService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ClassModifyServlet",urlPatterns = "/classModify")
public class ClassModifyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码格式
        request.setCharacterEncoding("UTF-8");
        // 获取参数并创建对象
        String modifyID = request.getParameter("modifyHiddenClassName");
        String className = request.getParameter("modifyClassName");
        String classGrade = request.getParameter("modifyGrade");
        String classTeacher = request.getParameter("modifyTeacher");
        String classSlogan = request.getParameter("modifySlogan");
        String classNumber = request.getParameter("modifyNumber");
        SysClass sysClass = new SysClass(className,classGrade,classTeacher,classSlogan,Integer.parseInt(classNumber));
        ClassService classService = new ClassService();
        // 计算班级人数
        //classService.claculateClassNumber(sysClass);
        // 修改班级信息
        int i = classService.modifyClassService(sysClass);
        if (i == 1){
            request.setAttribute("modifySuccess","modifySuccess");
            // 实现服务器跳转  共享request和response对象
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("class");
            requestDispatcher.forward(request, response);
            if (request.getAttribute("modifyError") != null){
                request.removeAttribute("modifyError");
            }
        }else {
            request.setAttribute("modifyError","modifyError");
            // 实现服务器跳转  共享request和response对象
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("class");
            requestDispatcher.forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
